package com.wjt.homestayms.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.util.HashMap;
//声明这是一个配置类
@Configuration
public class DruidConfiguration {
    //将刚才配置的yml文件和配置类绑定
    @ConfigurationProperties(prefix = "spring.datasource.druid")
    //注入到Bean中，交给spring进行管理
    @Bean
    public DataSource druidDataSource(){
        return new DruidDataSource();
    }
    //配置druid的监控页功能
//    @Bean
//    public ServletRegistrationBean statViewServlet(){
//        ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");
//        //后台需要有人登录，账号密码配置
//        HashMap<String, String> initParameters = new HashMap<>();
//        //加配置
//        //注意：参数名字是固定的，为loginUsername 和loginPassword
//        initParameters.put("loginUsername","admin");
//        initParameters.put("loginPassword","qq573261");
//
//        //允许谁能访问
//        initParameters.put("allow","");//value参数为空，所有人能访问
//
//        //禁止谁能访问
////        initParameters.put("my","");//value为ip地址，配置了为拒绝访问
//        bean.setInitParameters(initParameters);//设置初始化参数

//        return bean;
//    }
}
